import 'package:aero_frisbee/app/model/af_block_list_model/af_block_list_model.dart';
import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:aero_frisbee/pages/modues/message/pages/followed/view_model/af_followed_list_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';

import 'view_model/af_conversation_list_view_model.dart';
import 'views/af_conversation_list_cell.dart';

class AfConversationListScreen extends ConsumerStatefulWidget {
  const AfConversationListScreen({super.key});
  @override
  ConsumerState<ConsumerStatefulWidget> createState() =>
      _AfConversationListScreenState();
}

class _AfConversationListScreenState
    extends ConsumerState<AfConversationListScreen> {
  @override
  Widget build(BuildContext context) {
    final blockList = ref
        .watch(getBlockListFutureProvider)
        .maybeWhen(
          orElse: () => List<AfBlockListModel>.from([]),
          data: (data) => data,
        )
        .map((e) => e.broadcasterId ?? '')
        .toList();
    return ref.watch(afConversationListServiceProvider).maybeWhen(
          orElse: () => Container(),
          loading: () => const Center(child: CircularProgressIndicator()),
          data: (data) => _buildBody(data
              .where((e) => !blockList.contains(e.targetId ?? ''))
              .toList()),
        );
  }

  _buildBody(List<RCIMIWConversation> data) {
    if (data.isEmpty) {
      return Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
        ),
        padding: const EdgeInsets.all(20),
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Align(
                alignment: Alignment.topCenter,
                child: Assets.icons.profileNoData.image(width: 150),
              ),
              Text(
                'No Data',
                style: TextStyle(
                  color: const Color(0xff000000).withOpacity(0.3),
                  fontWeight: FontWeight.normal,
                  fontSize: 16,
                ),
                textAlign: TextAlign.left,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
              ),
              const SizedBox(
                height: 120,
              ),
            ],
          ),
        ),
      );
    }
    return ListView.separated(
      padding: const EdgeInsets.only(top: 20),
      itemBuilder: (context, index) {
        return AfConversationListCell(
          item: data[index],
        );
      },
      separatorBuilder: (context, index) => const SizedBox(
        height: 0,
      ),
      itemCount: data.length,
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(AfConversationListScreen oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
